/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.PlanCheckHistoryScore;
import org.springblade.modules.admin.entity.PlanScoreDimension;
import org.springblade.modules.admin.entity.PlanScoreDimensionItem;
import org.springblade.modules.admin.service.IMaintainContractTypeService;
import org.springblade.modules.admin.service.IPlanCheckHistoryScoreService;
import org.springblade.modules.admin.service.IPlanScoreDimensionItemService;
import org.springblade.modules.admin.vo.PlanScoreDimensionVO;
import org.springblade.modules.admin.mapper.PlanScoreDimensionMapper;
import org.springblade.modules.admin.service.IPlanScoreDimensionService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.List;

/**
 * 评分维度表 服务实现类
 *
 * @author ysq
 * @since 2024-01-15
 */
@AllArgsConstructor
@Service
public class PlanScoreDimensionServiceImpl extends BaseServiceImpl<PlanScoreDimensionMapper, PlanScoreDimension> implements IPlanScoreDimensionService {

	private final IPlanScoreDimensionItemService planScoreDimensionItemService;
	private final IPlanCheckHistoryScoreService planCheckHistoryScoreService;
	private final IMaintainContractTypeService maintainContractTypeService;

	@Override
	public IPage<PlanScoreDimensionVO> selectPlanScoreDimensionPage(IPage<PlanScoreDimensionVO> page, PlanScoreDimensionVO planScoreDimension) {
		return page.setRecords(baseMapper.selectPlanScoreDimensionPage(page, planScoreDimension));
	}

	@Override
	public boolean customSave(PlanScoreDimension planScoreDimension) {
		if (Func.isNotEmpty(planScoreDimension.getDimensionName())) {
			String pinyin = PinyinUtil.getFirstLetter(planScoreDimension.getDimensionName().substring(0,2), "");
			planScoreDimension.setPrefixCode(pinyin.toUpperCase());
		}
		return save(planScoreDimension);
	}

	@Override
	public boolean initScoreDimension(String tenantIds) {
		List<String> tenantIdList = Func.toStrList(tenantIds);
		// 查询000000租户数据，复制一份
		List<PlanScoreDimension> dimensionList = this.list(Wrappers.<PlanScoreDimension>lambdaQuery().eq(PlanScoreDimension :: getTenantId, BladeConstant.ADMIN_TENANT_ID));
		for (String tenantId : tenantIdList) {
			maintainContractTypeService.addTenantInit(tenantId);

			dimensionList.forEach(dimension -> {
				PlanScoreDimension planScoreDimension = new PlanScoreDimension();
				planScoreDimension.setPrefixCode(dimension.getPrefixCode());
				planScoreDimension.setDimensionName(dimension.getDimensionName());
				planScoreDimension.setTenantId(tenantId);
				this.save(planScoreDimension);

				// 评分细项
				List<PlanScoreDimensionItem> dimensionItemList = planScoreDimensionItemService.list(Wrappers.<PlanScoreDimensionItem>lambdaQuery().eq(PlanScoreDimensionItem :: getScoreDimensionId, dimension.getId()));
				dimensionItemList.forEach(dimensionItem -> {
					PlanScoreDimensionItem planScoreDimensionItem = new PlanScoreDimensionItem();
					planScoreDimensionItem.setScoreDimensionId(planScoreDimension.getId());
					planScoreDimensionItem.setScoreLevel(dimensionItem.getScoreLevel());
					planScoreDimensionItem.setDescription(dimensionItem.getDescription());
					planScoreDimensionItem.setScore(dimensionItem.getScore());
					planScoreDimensionItem.setRequired(1);
					planScoreDimensionItem.setTenantId(tenantId);
					planScoreDimensionItemService.save(planScoreDimensionItem);
				});
			});
		}

		return true;
	}

	@Override
	public long editCheck(Long id) {
		long count = planCheckHistoryScoreService.list(Wrappers.<PlanCheckHistoryScore>lambdaQuery().eq(PlanCheckHistoryScore :: getScoreDimensionId, id))
			.stream().map(t -> t.getPlanId()).distinct().count();
		return count;
	}
}
